#region System
using System;
using System.Collections.Generic;
using System.IO;
using CF.Framework.Engine.Drawing;
using CF.Framework.Engine.Misc;
using CF.Framework.Engine.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace CF.Framework.Engine.Controls
{
	/// <summary>
	/// Used to set the style of a GameBackground
	/// </summary>
	public enum BackgroundStyle
	{
		/// <summary>
		/// Stretches the background to fit the screen
		/// </summary>
		Stretch,
		/// <summary>
		/// Tiles the background to fit the screen
		/// </summary>
		Tiled,
		/// <summary>
		/// Centers the background on the screen
		/// </summary>
		Center,
		/// <summary>
		/// Does not tile, center, or stretch the background
		/// </summary>
		None
	}

	public class Background : ContextContainer
	{
		/// <summary>
		/// The background style of the GameBackground
		/// </summary>
		public BackgroundStyle Style { get; set; }

		/// <summary>
		/// Initializes a new GameBackground
		/// </summary>
		/// <param name="backgroundImage">The image to use as the background</param>
		public Background()
		{
		}

		protected override void OnUpdate(GameTime gameTime)
		{
			base.OnUpdate(gameTime);
		}

		protected override void OnDraw(GameTime gameTime, SpriteBatch spriteBatch)
		{
			base.OnDraw(gameTime, spriteBatch);

			// Draw the background
			if (Style == BackgroundStyle.Stretch)
			{
				// Draws the background stretched to fit the screen
				if (OpacityMask != null)
				{
					spriteBatch.Draw(Context, this.ScaledPosition, new Rectangle(0, 0, ScreenManager.ScreenWidth, ScreenManager.ScreenHeight), Color.White, 0, Vector2.Zero, this.Scale, SpriteEffects.None, 0);

					if (Opacity != 255)
					{
						spriteBatch.Draw(this.OpacityMask, this.ScaledPosition, this.ScaledRectangle, new Color(255, 255, 255, (byte)MathHelper.Clamp(255 - this.Opacity, 0, 255)), 0, Vector2.Zero, this.Scale, SpriteEffects.None, 0);
					}
				}
				else
				{
					spriteBatch.Draw(Context, this.ScaledPosition, null, new Color(255, 255, 255, (byte)MathHelper.Clamp(this.Opacity, 0, 255)), 0, Vector2.Zero, this.Scale, SpriteEffects.None, 0);
				}
			}
			else if (Style == BackgroundStyle.Tiled)
			{
				// Tiles the background based on the offset
				if (OpacityMask != null)
				{
					for (int currentx = (int)Position.X; currentx < this.Width; currentx += Context.Width)
					{
						for (int currenty = (int)Position.Y; currenty < this.Height; currenty += Context.Height)
						{
							spriteBatch.Draw(Context, new Vector2(currentx, currenty), null, Color.White, 0, Vector2.Zero, this.Scale, SpriteEffects.None, 1);
						}
					}

					if (Opacity != 255)
					{
						spriteBatch.Draw(this.OpacityMask, new Vector2(0, 0), null, new Color(255, 255, 255, (byte)MathHelper.Clamp(this.Opacity, 0, 255)), 0, Vector2.Zero, this.Scale, SpriteEffects.None, 0);
					}
				}
				else
				{
					for (int currentx = (int)Position.X; currentx < this.Width; currentx += Context.Width)
					{
						for (int currenty = (int)Position.Y; currenty < this.Height; currenty += Context.Height)
						{
							spriteBatch.Draw(Context, new Vector2(currentx, currenty), null, new Color(255, 255, 255, (byte)MathHelper.Clamp(this.Opacity, 0, 255)), 0, Vector2.Zero, this.Scale, SpriteEffects.None, 1);
						}
					}
				}
			}
			else
			{
				if (Style == BackgroundStyle.Center)
				{
					Position = new Vector2(this.Width / 2 - Context.Width / 2, this.Height / 2 - Context.Height / 2);
				}

				if (OpacityMask != null)
				{
					spriteBatch.Draw(Context, this.ScaledPosition, null, Color.White, 0, Vector2.Zero, this.Scale, SpriteEffects.None, 0);

					if (Opacity != 255)
					{
						spriteBatch.Draw(this.OpacityMask, this.ScaledPosition, this.ScaledRectangle, new Color(255, 255, 255, (byte)MathHelper.Clamp(this.Opacity, 0, 255)), 0, Vector2.Zero, this.Scale, SpriteEffects.None, 0);
					}
				}
				else
				{
					spriteBatch.Draw(Context, this.ScaledPosition, null, new Color(255, 255, 255, (byte)MathHelper.Clamp(this.Opacity, 0, 255)), 0, Vector2.Zero, this.Scale, SpriteEffects.None, 0);
				}
			}
		}
	}
}
